//
//  KLAddViewController.m
//  Koala
//
//  Created by cao pan on 11/23/14.
//  Copyright (c) 2014 Anonymous. All rights reserved.
//

#import "KLAddViewController.h"
#import "SLPhotoLoadUtil.h"

@interface KLAddViewController ()
{
    UITextField * _titleTextField;
    UITextField * _tagTextField;
    UIButton * _coverButton;
    UIView * _listView ;
    NSMutableArray * _tagButtonArr;
}
@end

@implementation KLAddViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle:@"Add"];
        _tagButtonArr = [[NSMutableArray alloc] init];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setLeftButton:nil normalImage:@"btn_back_normal" highlightImage:@"btn_back_press"] ;
    [self setRightButton:@"深度定制" normalImage:@"btn_add_right" highlightImage:@""];
    
    [self titleTextField];
    [self coverButton];
    [self tagListView];
    [self summitButton];
    [self addListButtonView:210];
    [_listView setHidden:YES];
}



- (void)rightNavButtonClicked
{
    Alert(@"提示", @"暂未开放，敬请期待");
}


- (void)titleTextField
{
    _titleTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 20, 290, 30)];
    [_titleTextField setFont:[UIFont systemFontOfSize:15.0f]];
    [_titleTextField setPlaceholder:@"  标题:"];
    [_titleTextField.layer setBorderColor:RGBColor10Bit(241, 241, 241, 1).CGColor];
    [_titleTextField.layer setBorderWidth:1];
    [self.view addSubview:_titleTextField];
}


- (void)coverButton
{
    _coverButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_coverButton setFrame:CGRectMake(15, 70, 90, 90)];
    [_coverButton setBackgroundImage:[UIImage imageNamed:@"btn_add_cover"] forState:UIControlStateNormal];
    [_coverButton addTarget:self action:@selector(selectCoverAction:) forControlEvents:UIControlEventTouchUpInside];
    [_coverButton setClipsToBounds:YES];
    [self.view addSubview:_coverButton];
}


- (void)tagListView
{
    _tagTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 180, 275, 30)];
    [_tagTextField setFont:[UIFont systemFontOfSize:15.0f]];
    [_tagTextField.layer setBorderColor:RGBColor10Bit(78, 139, 217, 1).CGColor];
    [_tagTextField.layer setBorderWidth:1];
    [_tagTextField setClearsOnBeginEditing:YES];
    [_tagTextField setDelegate:self];
    [self.view addSubview:_tagTextField];
    
    UIButton * addTagButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [addTagButton setBackgroundImage:[UIImage imageNamed:@"btn_add_tag"] forState:UIControlStateNormal];
    [addTagButton setFrame:CGRectMake(275, 180, 30, 30)];
    [addTagButton addTarget:self action:@selector(addTagAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addTagButton];
    
}


- (void)addListButtonView:(float)yOffset
{
    float buttonHeight = 20;
    NSUInteger buttonNumber = 3;
    
    _listView = [[UIView alloc] initWithFrame:CGRectMake(15, yOffset, 260, buttonHeight * buttonNumber)];
    [_listView setBackgroundColor:[UIColor whiteColor]];
    [_listView.layer setBorderWidth:1];
    [_listView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    for (int i = 0 ; i < 3; i++) {
        UIButton * listButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [listButton setFrame:CGRectMake(0, i * buttonHeight, 290, buttonHeight)];
        [listButton setTitle:[[self listNamedArr] objectAtIndex:i] forState:UIControlStateNormal];
        [listButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [[listButton titleLabel] setFont:[UIFont systemFontOfSize:13.0f]];
        [listButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -170, 0, 0)];
        [listButton addTarget:self action:@selector(listButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [listButton setTag:i];
        [_listView addSubview:listButton];
    }
    
    [self.view addSubview:_listView];
    [self.view bringSubviewToFront:_listView];
}

- (NSArray *)listNamedArr
{
    NSArray * titleArr = @[@"豆瓣正在热映",@"原创数字作品",@"豆瓣近期热门"];
    return titleArr;
}

- (void)listButtonClicked:(UIButton *)sender
{
    NSUInteger  index = sender.tag;
    [_tagTextField setText:[[self listNamedArr] objectAtIndex:index]];
    [_listView setHidden:YES];
}



- (void)summitButton
{
    UIButton * summitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [summitButton setFrame:CGRectMake(15, 300, 290, 35)];
    [summitButton setBackgroundImage:[UIImage imageNamed:@"btn_summit"] forState:UIControlStateNormal];
    [summitButton addTarget:self action:@selector(summitAction:) forControlEvents:UIControlEventTouchUpInside];
    [summitButton setTitle:@"提交" forState:UIControlStateNormal];
    [[summitButton titleLabel] setFont:[UIFont boldSystemFontOfSize:15.0f]];
    [self.view addSubview:summitButton];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length == 0) {
        return YES;
    }
    [_listView setHidden:NO];
    [self.view bringSubviewToFront:_listView];
    return YES;
}


- (void)selectCoverAction:(id)sender
{
    UIButton * coverButton = (UIButton *)sender;
    [SLPhotoLoadUtil showSelectAction:self block:^(UIImage *image) {
        [coverButton setBackgroundImage:image forState:UIControlStateNormal];
    }];
}


- (void)addTagAction:(id)sender
{
    [self addTagButton:_tagTextField.text];
    [_tagTextField setText:nil];
}

- (void)summitAction:(id)sender
{
    Alert(@"提示", @"添加成功");
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)addTagButton:(NSString *)text
{
    UIButton * tagButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tagButton setTitle:text forState:UIControlStateNormal];
    [tagButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [tagButton.layer setBorderColor:RGBColor10Bit(78, 139, 217, 1).CGColor];
    [tagButton.layer setBorderWidth:1];
    [tagButton setFrame:CGRectMake(15 + _tagButtonArr.count % 2 * 150, floor(_tagButtonArr.count / 2 * 40) + 220, 120, 30)];
    [self.view addSubview:tagButton];
    [_tagButtonArr addObject:tagButton];
    
    UIButton * deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setFrame:CGRectMake(tagButton.origin.x + 40, tagButton.origin.y, 30, 30)];
    [deleteButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
 //   [tagButton addSubview:deleteButton];
    
}



@end
